﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity;
using System.Linq;
using System.Reflection;
using System.Web;

namespace TheCup
{
    [Table("tee")]
    public class Tee
    {
        [Key]
        public int Id { get; set; }

        [Column("active")]
        [Display(Name="Aktiv")]
        public bool IsActive { get; set; }

        public int CourseId { get; set; }

        [ForeignKey("CourseId")]
        public virtual Course Course { get; set; }

        [Required]
        public string Name { get; set; }

        [NotMapped]
        public string DisplayName
        {
            get
            {
                return string.Format(" {0} ({1}) - {2}", Name, Description, Gender);
            }
        }

        [Column("loop")]
        public string Description { get; set; }

        [Column("Sex")]
        [Required]
        public string Gender { get; set; }
                
        //[DisplayFormat(DataFormatString="{0:}")]
        [Required]
        [DisplayFormat(ConvertEmptyStringToNull = true, DataFormatString="{0:F1}")]
        public double CR { get; set; }

        [Required]
        [DisplayFormat(ConvertEmptyStringToNull=true)]
        public int Slope { get; set; }
        
        [DataType(DataType.Date)]
        public DateTime Created { get; set; }

        public int ParOut
        {
            get
            {
                return Par1 + Par2 + Par3 + Par4 + Par5 + Par6 + Par7 + Par8 + Par9;
            }
        }
        public int ParIn
        {
            get
            {
                return Par10 + Par11 + Par12 + Par13 + Par14 + Par15 + Par16 + Par17 + Par18;
            }
        }
        public int Par
        {
            get
            {
                return ParOut + ParIn;
            }
        }

        public void ApplyRules()
        {
            this.Name = this.Name == null ? null : this.Name.Trim();
            this.Description = this.Description == null ? null : this.Description.Trim();
            this.Gender = this.Gender == null ? null : this.Gender.Trim();
        }

        public int Par1 { get; set; }
        public int Par2 { get; set; }
        public int Par3 { get; set; }
        public int Par4 { get; set; }
        public int Par5 { get; set; }
        public int Par6 { get; set; }
        public int Par7 { get; set; }
        public int Par8 { get; set; }
        public int Par9 { get; set; }
        public int Par10 { get; set; }
        public int Par11 { get; set; }
        public int Par12 { get; set; }
        public int Par13 { get; set; }
        public int Par14 { get; set; }
        public int Par15 { get; set; }
        public int Par16 { get; set; }
        public int Par17 { get; set; }
        public int Par18 { get; set; }

        public int Idx1 { get; set; }
        public int Idx2 { get; set; }
        public int Idx3 { get; set; }
        public int Idx4 { get; set; }
        public int Idx5 { get; set; }
        public int Idx6 { get; set; }
        public int Idx7 { get; set; }
        public int Idx8 { get; set; }
        public int Idx9 { get; set; }
        public int Idx10 { get; set; }
        public int Idx11 { get; set; }
        public int Idx12 { get; set; }
        public int Idx13 { get; set; }
        public int Idx14 { get; set; }
        public int Idx15 { get; set; }
        public int Idx16 { get; set; }
        public int Idx17 { get; set; }
        public int Idx18 { get; set; }

        public Nullable<int> L1 { get; set; }
        public Nullable<int> L2 { get; set; }
        public Nullable<int> L3 { get; set; }
        public Nullable<int> L4 { get; set; }
        public Nullable<int> L5 { get; set; }
        public Nullable<int> L6 { get; set; }
        public Nullable<int> L7 { get; set; }
        public Nullable<int> L8 { get; set; }
        public Nullable<int> L9 { get; set; }
        public Nullable<int> L10 { get; set; }
        public Nullable<int> L11 { get; set; }
        public Nullable<int> L12 { get; set; }
        public Nullable<int> L13 { get; set; }
        public Nullable<int> L14 { get; set; }
        public Nullable<int> L15 { get; set; }
        public Nullable<int> L16 { get; set; }
        public Nullable<int> L17 { get; set; }
        public Nullable<int> L18 { get; set; }

        public Nullable<int> LengthOut
        {
            get
            {
                return L1 + L2 + L3 + L4 + L5 + L6 + L7 + L8 + L9;
            }
        }
        public Nullable<int> LengthIn
        {
            get
            {
                return L10 + L11 + L12 + L13 + L14 + L15 + L16 + L17 + L18;
            }
        }
        public Nullable<int> Length
        {
            get
            {
                return LengthOut + LengthIn;
            }
        }

        public int GetPropertyValue(string property, int hole)
        {
            PropertyInfo pi = this.GetPropertyInfo(property, hole);
            object value = pi.GetValue(this);
            return Int32.Parse(value == null ? "0" : value.ToString());
        }

        internal PropertyInfo GetPropertyInfo(string property, int hole)
        {
            return this.GetType().GetProperty(property + hole);
        }

        public void SetIdx(int hole, int value)
        {
            GetPropertyInfo("Idx", hole).SetValue(this, value);
        }

        public void SetPar(int hole, int value)
        {
            GetPropertyInfo("Par", hole).SetValue(this, value);
        }

        public int GetIdx(int hole)
        {
            return GetPropertyValue("Idx", hole);
        }

        public int GetPar(int hole)
        {
            return GetPropertyValue("Par", hole);
        }

        /// <summary>
        /// Written to allow return of null values, so views arent prefilled with 0's
        /// </summary>
        /// <param name="hole"></param>
        /// <returns></returns>
        public int? GetLength(int hole)
        {
            PropertyInfo pi = GetPropertyInfo("L", hole);
            return (int?)pi.GetValue(this);
        }
    }

}